package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

/**
function :统计一个文件中各个字符 的数量

*/
type charCounts struct {
	charCount  int
	numCount   int
	spaceCont  int
	otherCount int
}

func main() {
	var counts charCounts
	filePath := "advance/fileIO/testFiles/contfIle.txt"
	file, err := os.Open(filePath)
	if err != nil {
		fmt.Println("文件打开失败")
		return
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	for {
		data, err := reader.ReadString('\n')
		if err == io.EOF {
			break
		}
		//为了兼容中文，可以将str转换成[]rune
		data = string([]rune(data))
		for _, value := range data {
			switch {
			case value >= 'a' && value <= 'z':
				fallthrough
			case value >= 'A' && value <= 'Z':
				counts.charCount++
			case value == ' ' || value == '\t':
				counts.spaceCont++
			case value >= '0' && value <= '9':
				counts.numCount++
			default:
				counts.otherCount++
			}
		}
	}
	fmt.Printf("字母：%v个  数字：%v个  空格：%v个  其他：%v个",
		counts.charCount, counts.numCount, counts.spaceCont, counts.otherCount)
}
